package com.hhkj.study.api.index.sevice.biz.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hhkj.study.admin.article.entity.Article;
import com.hhkj.study.admin.article.enums.BannerEnum;
import com.hhkj.study.admin.article.model.req.ArticleCatalogReq;
import com.hhkj.study.admin.article.model.res.ArticleBannerRes;
import com.hhkj.study.admin.article.model.res.ArticleCatalogRes;
import com.hhkj.study.admin.article.model.res.ArticleExamFlowRes;
import com.hhkj.study.admin.article.model.res.ArticleRes;
import com.hhkj.study.admin.article.service.base.ArticleService;
import com.hhkj.study.admin.article.service.biz.ArticleBannerBizService;
import com.hhkj.study.admin.article.service.biz.ArticleBizService;
import com.hhkj.study.admin.article.service.biz.ArticleCatalogBizService;
import com.hhkj.study.admin.article.service.biz.ArticleExamFlowBizService;
import com.hhkj.study.admin.config.entity.ConfigBase;
import com.hhkj.study.admin.config.entity.ConfigBaseValue;
import com.hhkj.study.admin.config.entity.ConfigBook;
import com.hhkj.study.admin.config.model.res.ConfigBookRes;
import com.hhkj.study.admin.config.model.res.ConfigCompanyRes;
import com.hhkj.study.admin.config.model.res.ConfigLinkRes;
import com.hhkj.study.admin.config.model.res.ConfigWelcomeRes;
import com.hhkj.study.admin.config.service.base.ConfigBaseService;
import com.hhkj.study.admin.config.service.biz.ConfigBookBizService;
import com.hhkj.study.admin.config.service.biz.ConfigCompanyBizService;
import com.hhkj.study.admin.config.service.biz.ConfigLinkBizService;
import com.hhkj.study.admin.config.service.biz.ConfigWelcomeBizService;
import com.hhkj.study.admin.course.entity.Course;
import com.hhkj.study.admin.course.entity.CourseAssocResourceStat;
import com.hhkj.study.admin.course.entity.CourseResourceInfo;
import com.hhkj.study.admin.course.enums.CourseAssocResourceTypeEnum;
import com.hhkj.study.admin.course.model.res.CourseRes;
import com.hhkj.study.admin.course.service.biz.CourseBizService;
import com.hhkj.study.admin.dict.common.LevelCodeEnum;
import com.hhkj.study.admin.dict.model.res.DictMultiLevelRes;
import com.hhkj.study.admin.dict.service.biz.DictMultiLevelBizService;
import com.hhkj.study.admin.goods.model.podo.GoodsSkuPo;
import com.hhkj.study.admin.goods.model.res.GoodsRes;
import com.hhkj.study.admin.goods.service.biz.GoodsBizService;
import com.hhkj.study.admin.teacher.model.res.TeacherRes;
import com.hhkj.study.admin.teacher.service.biz.TeacherBizService;
import com.hhkj.study.api.goods.service.biz.ApiGoodsBizService;
import com.hhkj.study.api.index.model.req.*;
import com.hhkj.study.api.index.model.res.*;
import com.hhkj.study.api.index.sevice.biz.ApiIndexBizService;
import com.hhkj.study.common.cache.GlobalCache;
import com.hhkj.study.common.service.CommonService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.cont.SysConst;
import com.ruoyi.common.enums.DictCodeEnum;
import com.ruoyi.common.exception.GlobalException;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

/**
 * @Author luowu
 * @Date 2024/9/29 涵 21:18:38
 */
@Service
public class ApiIndexBizServiceImpl implements ApiIndexBizService {

    @Resource
    private ArticleBannerBizService articleBannerBizService;

    @Resource
    private DictMultiLevelBizService dictMultiLevelBizService;

    @Resource
    private ArticleCatalogBizService articleCatalogBizService;

    @Resource
    private ArticleBizService articleBizService;

    @Resource
    private ArticleService articleService;

    @Resource
    private GoodsBizService goodsBizService;

    @Resource
    private TeacherBizService teacherBizService;

    @Resource
    private ArticleExamFlowBizService articleExamFlowBizService;

    @Resource
    private ConfigWelcomeBizService configWelcomeBizService;

    @Resource
    private ConfigCompanyBizService configCompanyBizService;

    @Resource
    private ConfigLinkBizService configLinkBizService;

    @Resource
    private ConfigBookBizService configBookBizService;

    @Resource
    private ConfigBaseService configBaseService;

    @Resource
    private ApiGoodsBizService apiGoodsBizService;

    @Resource
    private CourseBizService courseBizService;

    @Resource
    private CommonService commonService;

    @Resource
    private GlobalCache globalCache;

    @Override
    public List<ApiArticleBannerListRes> bannerList(ApiArticleBannerListReq req) {
        // 设置默认参数
        setDefaultParam(req);
        List<ArticleBannerRes> list = articleBannerBizService.list(req);
        if (CollectionUtil.isNotEmpty(list)) {
            // 按照orderNum 升序排序
            CollectionUtil.sort(list, Comparator.comparing(ArticleBannerRes::getOrderNum, Comparator.nullsFirst(Comparator.naturalOrder())));
        }
        return BeanUtil.copyToList(list, ApiArticleBannerListRes.class);
    }

    @Override
    public List<ApiDictTreeListRes> dictTreeList(ApiDictTreeListReq req) {
        // 默认启用的状态
        if (ObjectUtil.isNull(req.getDisableFlag())) {
            req.setDisableFlag(SysConst.Flag.NO.getFlag());
        }
        // 考试动态需要默认设置课程类型以及省份
        boolean isExamTrend = StrUtil.equalsIgnoreCase(req.getTypeCode(), DictCodeEnum.EXAM_TREND.getCode());
        if (isExamTrend) {
            // 区域和课程类型查询默认值对应的id
            Pair<Long, Long> defaultProvinceAndCoursePair = getDefaultProvinceAndCourseType();
            req.setProvinceId(ObjectUtil.isNull(req.getProvinceId()) ? defaultProvinceAndCoursePair.getRight() : req.getProvinceId());
            req.setCourseType(ObjectUtil.isNull(req.getCourseType()) ? defaultProvinceAndCoursePair.getLeft() : req.getCourseType());
        } else {
            req.setProvinceId(null);
            req.setCourseType(null);
        }
        List<DictMultiLevelRes> dictResList = dictMultiLevelBizService.list(req);
        if (CollectionUtil.isEmpty(dictResList)) {
            return Lists.newArrayList();
        }
        List<ApiDictTreeListRes> resList = BeanUtil.copyToList(dictResList, ApiDictTreeListRes.class);
        // 考试动态特殊处理
        examTrendProcess(resList, req.getCourseType(), isExamTrend);
        Map<Long, List<ApiDictTreeListRes>> resListGroupByParentId = resList.stream()
                .collect(Collectors.groupingBy(ApiDictTreeListRes::getParentId));
        for (ApiDictTreeListRes res : resList) {
            if (resListGroupByParentId.containsKey(res.getId())) {
                res.setChildren(resListGroupByParentId.get(res.getId()));
            }
        }
        if (ObjectUtil.isNotNull(req.getParentId())) {
            return resList;
        }
        return resList.stream().filter(res -> LevelCodeEnum.SECOND.getCode().equals(res.getLevelCode())).collect(Collectors.toList());
    }

    @Override
    public List<ApiGoodsListRes> goodsList(ApiGoodsListReq req) {
        // 默认上架的商品
        req.setPublishedFlag(SysConst.Flag.IS.getFlag());
        // 默认意向
        if (ObjectUtil.isNull(req.getCategoryId())) {
            // 区域和课程类型查询默认值对应的id
            Pair<Long, Long> defaultProvinceAndCoursePair = getDefaultProvinceAndCourseType();
            req.setCategoryId(defaultProvinceAndCoursePair.getLeft());
        }
        req.setShowSkuFlag(SysConst.Flag.IS.getFlag());
        List<GoodsRes> goodsResList = goodsBizService.list(req);
        if (CollectionUtil.isEmpty(goodsResList)) {
            return Lists.newArrayList();
        }
        Set<Long> goodsIdSet = goodsResList.stream().map(GoodsRes::getId).collect(Collectors.toSet());
        // 商品和商品下的课程关系
        Map<Long, List<Course>> courseListByGoodIdListMap = apiGoodsBizService.getCourseListByGoodIdList(Lists.newArrayList(goodsIdSet));
        List<ApiGoodsListRes> apiGoodsResList = BeanUtil.copyToList(goodsResList, ApiGoodsListRes.class);
        if (CollectionUtil.isNotEmpty(apiGoodsResList)) {
            apiGoodsResList.sort(Comparator.comparing(ApiGoodsListRes::getOrderNum, Comparator.nullsFirst(Comparator.naturalOrder())));
            // 处理商品下的课时以及所有的课程耗时 设值给商品列表
            handlerGoodsCourseAndCalculateLesson(courseListByGoodIdListMap, apiGoodsResList);
        }

        return apiGoodsResList;
    }


    @Override
    public List<ApiArticleCatalogListRes> articleCatalogList() {
        ArticleCatalogReq req = new ArticleCatalogReq();
        req.setPublishedFlag(SysConst.Flag.IS.getFlag());
        List<ArticleCatalogRes> list = articleCatalogBizService.list(req);
        return BeanUtil.copyToList(list, ApiArticleCatalogListRes.class);
    }

    @Override
    public List<ApiArticleListRes> articleList(ApiArticleListReq req) {
        if (StrUtil.isBlank(req.getCatalogCode()) && ObjectUtil.isNull(req.getCatalogId())) {
            throw new GlobalException("资讯类别参数缺失");
        }
        // 如果有分类code以code为准
        if (StrUtil.isNotBlank(req.getCatalogCode())) {
            req.setCatalogId(null);
        }
        // 默认上架的轮播图
        if (ObjectUtil.isNull(req.getPublishedFlag())) {
            req.setPublishedFlag(SysConst.Flag.IS.getFlag());
        }
        // 如果有intentionalExamFlag标识且不为0 表示需要意向考试和地区搜索 否则不需要
        if (ObjectUtil.isNull(req.getIntentionalExamFlag()) || SysConst.Flag.IS.getFlag().equals(req.getIntentionalExamFlag())) {
            // 区域和课程类型查询默认值对应的id
            Pair<Long, Long> defaultProvinceAndCoursePair = getDefaultProvinceAndCourseType();
            req.setProvinceId(ObjectUtil.isNull(req.getProvinceId()) ? defaultProvinceAndCoursePair.getRight() : req.getProvinceId());
            req.setCourseType(ObjectUtil.isNull(req.getCourseType()) ? defaultProvinceAndCoursePair.getLeft() : req.getCourseType());
        } else {
            req.setCourseType(null);
            req.setProvinceId(null);
        }
        List<ArticleRes> articleResList = articleBizService.list(req);
        if (CollectionUtil.isNotEmpty(articleResList)) {
            articleResList = articleResList.stream()
                    .sorted(Comparator.comparing(ArticleRes::getOrderNum)
                            .thenComparing(ArticleRes::getPublishTime, Comparator.nullsFirst(Comparator.reverseOrder()))
                            .thenComparing(ArticleRes::getCreateTime, Comparator.nullsFirst(Comparator.reverseOrder())))
                    .collect(Collectors.toList());
        }
        return BeanUtil.copyToList(articleResList, ApiArticleListRes.class);
    }


    @Override
    public ApiArticleGetRes lookupArticle(Long id) {
        ArticleRes articleRes = articleBizService.getById(id);
        LambdaUpdateWrapper<Article> articleUpdate = new LambdaUpdateWrapper<>();
        articleUpdate.eq(Article::getId, id).set(Article::getReadAmount, articleRes.getReadAmount() + 1);
        this.articleService.update(articleUpdate);
        return BeanUtil.toBean(articleRes, ApiArticleGetRes.class);
    }

    @Override
    public List<ApiTeacherListRes> teacherList(ApiTeacherListReq req) {
        // 首页讲师列表需要设置推荐标识
        req.setRecommendFlag(SysConst.Flag.IS.getFlag());
        List<TeacherRes> list = teacherBizService.list(req);
        list = list.stream().sorted(Comparator.comparing(TeacherRes::getOrderNum, Comparator.nullsFirst(Comparator.naturalOrder()))).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(list)) {
            return Lists.newArrayList();
        }
        afterTeacherList(list);
        return BeanUtil.copyToList(list, ApiTeacherListRes.class);
    }


    @Override
    public List<ApiExamFlowListRes> examFlowList(ApiExamFlowListReq req) {
        // 默认上架
        if (ObjectUtil.isNull(req.getPublishedFlag())) {
            req.setPublishedFlag(SysConst.Flag.IS.getFlag());
        }
        List<ArticleExamFlowRes> examFlowResList = articleExamFlowBizService.list(req);
        // 按照orderNum 升序排序
        CollectionUtil.sort(examFlowResList, Comparator.comparing(ArticleExamFlowRes::getOrderNum));
        return BeanUtil.copyToList(examFlowResList, ApiExamFlowListRes.class);
    }

    @Override
    public List<ApiWelcomeListRes> welcomeList(ApiWelcomeListReq req) {
        // 默认上架
        if (ObjectUtil.isNull(req.getPublishedFlag())) {
            req.setPublishedFlag(SysConst.Flag.IS.getFlag());
        }
        List<ConfigWelcomeRes> welcomeList = configWelcomeBizService.list(req);
        // 按照orderNum 升序排序
        CollectionUtil.sort(welcomeList, Comparator.comparing(ConfigWelcomeRes::getOrderNum));
        return BeanUtil.copyToList(welcomeList, ApiWelcomeListRes.class);
    }

    @Override
    public List<ApiCompanyListRes> companyList(ApiCompanyListReq req) {
        // 默认上架
        if (ObjectUtil.isNull(req.getPublishedFlag())) {
            req.setPublishedFlag(SysConst.Flag.IS.getFlag());
        }
        List<ConfigCompanyRes> list = configCompanyBizService.list(req);
        // 按照orderNum 升序排序
        CollectionUtil.sort(list, Comparator.comparing(ConfigCompanyRes::getOrderNum));
        return BeanUtil.copyToList(list, ApiCompanyListRes.class);
    }

    @Override
    public List<ApiFriendLinkListRes> friendLinkList(ApiFriendLinkListReq req) {
        // 默认上架
        if (ObjectUtil.isNull(req.getPublishedFlag())) {
            req.setPublishedFlag(SysConst.Flag.IS.getFlag());
        }
        List<ConfigLinkRes> list = configLinkBizService.list(req);
        // 按照orderNum 升序排序
        CollectionUtil.sort(list, Comparator.comparing(ConfigLinkRes::getOrderNum));
        return BeanUtil.copyToList(list, ApiFriendLinkListRes.class);
    }

    @Override
    public List<ApiBookListRes> bookList(ApiBookListReq req) {
        // 默认上架
        if (ObjectUtil.isNull(req.getPublishedFlag())) {
            req.setPublishedFlag(SysConst.Flag.IS.getFlag());
        }
        List<ConfigBookRes> list = configBookBizService.list(req);
        // 按照orderNum 升序排序
        CollectionUtil.sort(list, Comparator.comparing(ConfigBook::getOrderNum));
        return BeanUtil.copyToList(list, ApiBookListRes.class);
    }

    @Override
    public Map<String, String> getConfig(ApiConfigReq req) {

        Map<String, String> configMap = Maps.newHashMap();
        if (ObjectUtil.isNotNull(req.getCourseType())) {
            Map<Long, String> dictMap = globalCache.cacheSimpleDictMap(Lists.newArrayList(DictCodeEnum.COURSE_TYPE.getCode()));
            configMap.put("courseTypeName", dictMap.get(req.getCourseType()));
        }

        LambdaQueryWrapper<ConfigBase> configBaseQuery = new LambdaQueryWrapper<>();
        configBaseQuery.eq(ObjectUtil.isNotNull(req.getCourseType()), ConfigBase::getCourseType, req.getCourseType()).apply(ObjectUtil.isNotNull(req.getProvinceId()),StrUtil.format("JSON_CONTAINS(province_ids, CAST({} AS CHAR)) = 1", req.getProvinceId()))
                .in(ConfigBase::getCode, Lists.newArrayList(DictCodeEnum.KF_CONFIG.getCode(), DictCodeEnum.COURSE_BASE_CONFIG.getCode()))
                .eq(ConfigBase::getDisableFlag, SysConst.Flag.NO.getFlag()).ne(ConfigBase::getParentId, SysConst.Flag.NO.getFlag());
        List<ConfigBase> configBaseList = configBaseService.list(configBaseQuery);
        if (CollectionUtil.isEmpty(configBaseList)) {
            return configMap;
        }
        Optional<ConfigBase> courseConfigOptional = configBaseList.stream().filter(e -> DictCodeEnum.COURSE_BASE_CONFIG.getCode().equals(e.getCode())).findFirst();
        if (courseConfigOptional.isPresent()) {
            List<ConfigBaseValue> configBaseValueList = courseConfigOptional.map(ConfigBase::getValue).map(e -> JSONUtil.toList(e, ConfigBaseValue.class)).orElse(Lists.newArrayList());
            configBaseValueList.forEach(e -> configMap.put(e.getKey(), e.getContent()));
        }
        if (ObjectUtil.isNotNull(req.getUserId())) {
            List<ConfigBase> kfConfigList = configBaseList.stream().filter(e -> DictCodeEnum.KF_CONFIG.getCode().equals(e.getCode())).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(kfConfigList)) {
                ConfigBase configBase = CollectionUtil.get(kfConfigList, new Random().nextInt(kfConfigList.size()));
                if (ObjectUtil.isNotNull(configBase) && StrUtil.isNotBlank(configBase.getExtra())) {
                    configMap.put("qyQrcode", commonService.getWeComQrUrl(req.getUserId(), configBase.getExtra(), req.getAddSource()));
                }

            }
        }
        return configMap;
    }

    @Override
    public ApiCourseRes courseDetail(Long id) {
        if (ObjectUtil.isNull(id)) {
            return null;
        }
        CourseRes courseRes = courseBizService.getById(id);
        return BeanUtil.copyProperties(courseRes, ApiCourseRes.class);
    }

    @Override
    public List<ApiConfigBaseListRes> getBaseConfig(ApiConfigBaseReq req) {
        LambdaQueryWrapper<ConfigBase> configBaseQuery = new LambdaQueryWrapper<>();
        configBaseQuery.eq(ObjectUtil.isNotNull(req.getCourseType()), ConfigBase::getCourseType, req.getCourseType())
                .apply(ObjectUtil.isNotNull(req.getProvinceId()), StrUtil.format("JSON_CONTAINS(province_ids, CAST({} AS CHAR)) = 1", req.getProvinceId()))
                .in(ConfigBase::getCode, req.getCode())
                .eq(ConfigBase::getDisableFlag, SysConst.Flag.NO.getFlag()).ne(ConfigBase::getParentId, SysConst.Flag.NO.getFlag());
        List<ConfigBase> configBaseList = configBaseService.list(configBaseQuery);
        return BeanUtil.copyToList(configBaseList, ApiConfigBaseListRes.class);
    }

    /**
     * 处理商品下的课时以及所有的课程耗时 设值给商品列表
     *
     * @param courseListByGoodIdListMap 商品和商品下的课程关系
     * @param apiGoodsResList              商品列表
     */
    private void handlerGoodsCourseAndCalculateLesson(Map<Long, List<Course>> courseListByGoodIdListMap, List<ApiGoodsListRes> apiGoodsResList) {
        apiGoodsResList.forEach(goodsRes -> {
            // 该商品下的课程列表
            List<Course> courseByGoodsList = courseListByGoodIdListMap.get(goodsRes.getId());
            Pair<Integer, Long> lessonCountAndDurationPair = calculateCourseNum(courseByGoodsList);
            goodsRes.setLessonCount(lessonCountAndDurationPair.getLeft());
            goodsRes.setLessonDuration(lessonCountAndDurationPair.getRight());
            List<GoodsSkuPo> skuList = goodsRes.getSkuList();
            if (CollectionUtil.isNotEmpty(skuList)) {
                Optional<GoodsSkuPo> minPriceSku = skuList.stream().min(Comparator.comparing(GoodsSkuPo::getRealPrice, Comparator.nullsFirst(Comparator.naturalOrder())));
                minPriceSku.ifPresent(sku -> goodsRes.setRealPrice(sku.getRealPrice()));
            }
        });
    }

    /**
     * 计算课程的相关统计数据，包括总课时数和总视频时长
     * 该方法接收一个Course对象列表，并针对每个课程计算其关联资源中的视频总数和总时长
     *
     * @param courseByGoodsList Course对象列表，每个Course对象代表一个课程
     * @return 返回一个Pair对象，包含总课时数和总视频时长如果列表为空，则返回null
     */
    private Pair<Integer, Long> calculateCourseNum(List<Course> courseByGoodsList) {
        AtomicInteger totalLessonCountAtomic = new AtomicInteger(0);
        AtomicLong totalVideoDurationAtomic = new AtomicLong(0L);
        if (CollectionUtil.isNotEmpty(courseByGoodsList)) {
            courseByGoodsList.forEach(course -> {
                // 统计课程数 课程时长
                List<CourseAssocResourceStat> courseAssocResourceStatList = Optional.ofNullable(course)
                        .map(Course::getAssocResourceStat)
                        .map(CourseResourceInfo::getCourseAssocResourceList)
                        .orElse(Lists.newArrayList());
                List<CourseAssocResourceStat> resourceStatList = courseAssocResourceStatList.stream().filter(e -> CourseAssocResourceTypeEnum.VIDEO.getCode().equals(e.getResourceType())).collect(Collectors.toList());
                CourseAssocResourceStat courseResourceStat = CollectionUtil.getFirst(resourceStatList);
                if (ObjectUtil.isNotNull(courseResourceStat)) {
                    if (ObjectUtil.isNotNull(courseResourceStat.getResourceCount())) {
                        totalLessonCountAtomic.updateAndGet(v -> v + courseResourceStat.getResourceCount());
                    }
                    if (ObjectUtil.isNotNull(courseResourceStat.getVideoTotalDuration())) {
                        totalVideoDurationAtomic.updateAndGet(v -> v + courseResourceStat.getVideoTotalDuration());
                    }
                }
            });
        }
        return Pair.of(totalLessonCountAtomic.get(), totalVideoDurationAtomic.get());
    }

    /**
     * 针对考试动态处理
     * @param resList
     */
    private void examTrendProcess(List<ApiDictTreeListRes> resList, Long courseType, boolean isExamTrend){
        if (!isExamTrend) {
            return;
        }
        // 考试动态idList
        List<Long> examTrendIdList = resList.stream().map(ApiDictTreeListRes::getId).collect(Collectors.toList());
        LambdaQueryWrapper<Article> articleQuery = new LambdaQueryWrapper<>();
        articleQuery.eq(Article::getExamStatusFlag, SysConst.Flag.IS.getFlag()).in(Article::getExamStatusId, examTrendIdList).apply(ObjectUtil.isNotNull(courseType), StrUtil.format("JSON_CONTAINS(course_types, CAST({} AS CHAR)) = 1", courseType));
        List<Article> articleList = articleService.list(articleQuery);
        // 关联动态资讯 资讯url的map集合
        Map<Long, Long> examTrendIdArticleMap = articleList.stream().collect(Collectors.toMap(Article::getExamStatusId, Article::getId, (o, n) -> o));
        resList.forEach(res -> res.setArticleId(examTrendIdArticleMap.get(res.getId())));
    }

    /**
     * 轮播图设置默认值
     * @param req
     */
    private void setDefaultParam(ApiArticleBannerListReq req){
        // 默认上架的轮播图
        if (ObjectUtil.isNull(req.getPublishedFlag())) {
            req.setPublishedFlag(SysConst.Flag.IS.getFlag());
        }
        if (StrUtil.equalsIgnoreCase(req.getCatalogCode(), BannerEnum.COURSE_CENTER.getCode())) {
            // 区域和课程类型查询默认值对应的id
            Pair<Long, Long> defaultProvinceAndCoursePair = getDefaultProvinceAndCourseType();
            req.setProvinceId(ObjectUtil.isNull(req.getProvinceId()) ? defaultProvinceAndCoursePair.getRight() : req.getProvinceId());
            req.setCourseType(ObjectUtil.isNull(req.getCourseType()) ? defaultProvinceAndCoursePair.getLeft() : req.getCourseType());
        } else {
            req.setProvinceId(null);
            req.setCourseType(null);
        }
    }


    /**
     * 默认省份和课程类型
     * @return
     */
    private Pair<Long, Long> getDefaultProvinceAndCourseType() {
        // 默认区域和课程类型
        Map<String, Long> idNameMap = globalCache.getIdByName(Lists.newArrayList(Constants.DEFAULT_PROVINCE, Constants.DEFAULT_COURSE_TYPE), Lists.newArrayList(DictCodeEnum.REGION.getCode(), DictCodeEnum.COURSE_TYPE.getCode()));
        return Pair.of(idNameMap.get(Constants.DEFAULT_COURSE_TYPE), idNameMap.get(Constants.DEFAULT_PROVINCE));
    }


    /**
     * 根据教师列表中的教师范围映射列表生成教师教授内容字符串
     * 此方法旨在处理和丰富教师对象的数据，以便于后续处理或展示
     * @param list 教师响应对象列表，此列表中的每个教师对象都包含其教师范围映射列表
     */
    private void afterTeacherList(List<TeacherRes> list) {
        list.forEach(teacherRes -> {
            List<Map<String, Object>> teacherScopeMap = teacherRes.getTeacherScopeMap();
            if (CollectionUtil.isEmpty(teacherScopeMap)) {
                return;
            }
            LinkedHashSet<String> teacherScopeSet = teacherScopeMap.stream().filter(e -> ObjectUtil.isNotNull(e) && ObjectUtil.isNotNull(e.get("value")))
                    .map(map -> (String) map.get("value"))
                    .map(value -> value.split(StrUtil.DASHED)[0])
                    .collect(Collectors.toCollection(LinkedHashSet::new));
            teacherRes.setTeachScopeContent(CollectionUtil.join(teacherScopeSet, Constants.VERTICAL_LINE));

        });
    }
}
